// Main application entry point
// 主应用程序入口点
const PavilionMCPClient = require('./mcp-client/PavilionMCPClient');
const WaterQualityAgent = require('./agents/WaterQualityAgent');
const ChemicalDosageAgent = require('./agents/ChemicalDosageAgent');

// Create the main MCP client
// 创建主MCP客户端
const mcpClient = new PavilionMCPClient();

// Create and register agents
// 创建并注册Agent
const waterQualityAgent = new WaterQualityAgent();
const chemicalDosageAgent = new ChemicalDosageAgent();

mcpClient.registerAgent('waterQualityAgent', waterQualityAgent);
mcpClient.registerAgent('chemicalDosageAgent', chemicalDosageAgent);

// Initialize the system
// 初始化系统
mcpClient.initialize();

// Start coordination
// 开始协调
mcpClient.run();

// Periodically make decisions based on data
// 定期根据数据做出决策
setInterval(() => {
  // Get latest water quality data to make decisions
  // 获取最新的水质数据以做出决策
  const waterQualityData = mcpClient.getDataFromServer('waterQualityServer', 'waterQuality');
  
  if (waterQualityData) {
    // Have the chemical dosage agent make adjustments based on water quality
    // 让化学加药Agent根据水质进行调整
    chemicalDosageAgent.adjustDosage(waterQualityData);
    
    // Calculate and display efficiency
    // 计算并显示效率
    const efficiency = chemicalDosageAgent.getDosageEfficiency(waterQualityData);
    if (efficiency) {
      console.log('[ChemicalDosageAgent] Dosage efficiency:', {
        turbidity: `${efficiency.turbidityEfficiency.toFixed(1)}%`,
        chlorine: `${efficiency.chlorineEfficiency.toFixed(1)}%`,
        overall: `${efficiency.overallEfficiency.toFixed(1)}%`
      });
      
      console.log('[ChemicalDosageAgent] 加药效率:', {
        turbidity: `${efficiency.turbidityEfficiency.toFixed(1)}% (浊度)`,
        chlorine: `${efficiency.chlorineEfficiency.toFixed(1)}% (氯气)`,
        overall: `${efficiency.overallEfficiency.toFixed(1)}% (总体)`
      });
    }
  }
  
  // Clear resolved alerts occasionally
  // 偶尔清除已解决的警报
  waterQualityAgent.clearResolvedAlerts();
}, 10000);

console.log('Pavilion MCP System started');
console.log('Pavilion MCP系统已启动');
console.log('=====================================');
console.log('System Features:');
console.log('系统特性：');
console.log('- Real-time water quality monitoring');
console.log('- 实时水质监测');
console.log('- Automated chemical dosage control');
console.log('- 自动化化学加药控制');
console.log('- Alert generation for out-of-range parameters');
console.log('- 超出范围参数的警报生成');
console.log('- Efficiency tracking and optimization');
console.log('- 效率跟踪和优化');
console.log('=====================================');